package com.taotao.content.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.taotao.common.pojo.EasyUITreeNode;
import com.taotao.common.pojo.TaotaoResult;
import com.taotao.content.service.ContentCategoryService;
import com.taotao.mapper.TbContentCategoryMapper;
import com.taotao.pojo.TbContentCategory;
import com.taotao.pojo.TbContentCategoryExample;
import com.taotao.pojo.TbContentCategoryExample.Criteria;

@Service
public class ContentCategoryServiceImpl implements ContentCategoryService{
	//注入dao
	@Autowired
	private TbContentCategoryMapper contentCategoryMapper;
	@Override
	public List<EasyUITreeNode> getContentCategoryList(Long parentId) {
		TbContentCategoryExample example = new TbContentCategoryExample();
		Criteria criteria = example.createCriteria();
		criteria.andParentIdEqualTo(parentId);
		List<TbContentCategory> list = contentCategoryMapper.selectByExample(example);
		//封装参数
		List<EasyUITreeNode> resultList = new ArrayList<>();
		for (TbContentCategory tbContentCategory : list) {
			EasyUITreeNode node = new EasyUITreeNode();
			node.setId(tbContentCategory.getId());
			node.setText(tbContentCategory.getName());
			node.setState(tbContentCategory.getIsParent()?"closed":"open");
			resultList.add(node);
		}
		return resultList;
	}

	@Override
	public TaotaoResult insertContentCat(Long parentId, String name) {
		//封装插入节点数据
		TbContentCategory contentCategory = new TbContentCategory();
		contentCategory.setParentId(parentId);
		contentCategory.setName(name);
		//默认值为1
		contentCategory.setSortOrder(1);
		//新添加的节点都是叶子节点
		contentCategory.setIsParent(false);
		//可选值:1(正常),2(删除)
		contentCategory.setStatus(1);
		contentCategory.setCreated(new Date());
		contentCategory.setUpdated(new Date());
		contentCategoryMapper.insert(contentCategory);
		//查询添加节点的父节点是否为父节点
		TbContentCategory parentNode = contentCategoryMapper.selectByPrimaryKey(parentId);
		if (! parentNode.getIsParent()) {
			parentNode.setIsParent(true);
			//更新修改的父节点
			contentCategoryMapper.updateByPrimaryKey(parentNode);
		}
		//封装返回的数据
		return TaotaoResult.ok(contentCategory);
	}

	@Override
	public void updateContentCat(Long id,String name) {
		TbContentCategory contentCategory = contentCategoryMapper.selectByPrimaryKey(id);
		contentCategory.setUpdated(new Date());
		contentCategory.setName(name);
		contentCategoryMapper.updateByPrimaryKey(contentCategory);
	}

	@Override
	public TaotaoResult delContentCat(Long id) {
		//先判断此节点是否为叶子节点，不是叶子节点则不能删除
		TbContentCategory contentCategory = contentCategoryMapper.selectByPrimaryKey(id);
		Long parentId = contentCategory.getParentId();
		//不是叶子节点
		if(! contentCategory.getIsParent()){
			//删除
			contentCategoryMapper.deleteByPrimaryKey(id);
			//查询已删除的子节点的父节点是否还有子节点
			TbContentCategoryExample example = new TbContentCategoryExample();
			Criteria criteria = example.createCriteria();
			criteria.andParentIdEqualTo(parentId);
			List<TbContentCategory> list = contentCategoryMapper.selectByExample(example);
			//若查询出来list长度为0,则此父节点无子节点存在应改变其状态
			if( ! (list != null && list.size() > 0)){
				TbContentCategory category = contentCategoryMapper.selectByPrimaryKey(parentId);
				category.setUpdated(new Date());
				category.setIsParent(false);
				contentCategoryMapper.updateByPrimaryKey(category);
			}
			return TaotaoResult.build(1, "删除成功");
		}
		
		return TaotaoResult.build(0, "删除失败,不能删除文件夹");
	}
}
